﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WorkNodeFramework
{
    internal class TableMap
    {
        DataProvider provider;
        Dictionary<string, TableMapItem> maps = null;

        public TableMap(DataProvider provider) {
            this.provider = provider;
        }

        Dictionary<string, TableMapItem> Maps {
            get {
                if (maps == null)
                    Load();
                return maps;
            }
        }

        void ReLoad()
        {
            maps = null;
            Load();
        }

        void Load() {
            if (maps == null) {
                maps = new Dictionary<string, TableMapItem>();
                List<TableMapItem> items = new Select(provider)
                    .From("TableMap")
                    .ExecuteTypedList<TableMapItem>();
                foreach (TableMapItem item in items)
                    maps[item.Path] = item;
            }
        }

        public TableMapItem Find(string path) { 
            TableMapItem find =null;
            Maps.TryGetValue(path, out find);
            return find;
        }

        public TableMapItem Create<T>(string path)
            where T:WorkNode
        {
            WorkNodeTypeAttribute wnType = WorkNodeUtility.GetWorkNodeType<T>();
            TableMapItem mi = new TableMapItem();
            mi.Path = path;
            mi.WorkNode = wnType.Name;
            mi.Properties = wnType.Properties;
            mi.LangProperties = wnType.LangProperties;
            mi.MaxRow = -1;
            return mi;
        }

        public TableMapItem FindNearest(string path) {
            TableMapItem item = Find(path);
            if (item != null)
                return item;
            int index = path.LastIndexOf('/');
            if (index == -1)
                return null;
            path = path.Substring(0, index);
            return FindNearest(path);
        }

        public void SetMap(TableMapItem item) {
            TableMapItem find = Find(item.Path);
            if (find != null)
                Update(item);
            else
                Insert(item);
            //=========================
            Maps[item.Path] = item;
        }

        void Insert(TableMapItem item) { 
            ITable table = provider.GetTable("TableMap");
            new Insert(table)
                .Set(table["Path"], item.Path)
                .Set(table["WorkNode"], item.WorkNode)
                .Set(table["Properties"], item.Properties)
                .Set(table["LangProperties"], item.LangProperties)
                .Set(table["MaxRow"], item.MaxRow)
                .Execute();
        }

        void Update(TableMapItem item) { 
            ITable table = provider.GetTable("TableMap");
            new Update(table)
                .Set(table["WorkNode"], item.WorkNode)
                .Set(table["Properties"], item.Properties)
                .Set(table["LangProperties"], item.LangProperties)
                .Set(table["MaxRow"], item.MaxRow)
                .Where(WHERE.DO(table["Path"]).IsEqualsTo(item.Path))
                .Execute();
        }
    }
}
